﻿using FastCloud.Base.Config;
using FastIOC.Attribute;
using FastORM.Applicaiton;
using FastORM.Entity;
using FastORM.Enum;
using FastORM.Mapping;
using FastUtil.Config;
using FastUtil.Convert;
using FastUtil.Data;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using static FastIOC.Enum.CotainerEnum;

namespace FastCloud.Base.Desgin
{
    [Component(Scope = ComponentScope.Request)]
    public class CommonDTO
    {
        private CommonDao @CommonDao;
        private List<ParamEntity> ParamList;
        public CommonDTO()
        {
            @CommonDao = new CommonDao(DataBaseClass.MySql, AppConfigUtil.Configuration["ConnectionStrings:Cloud_ConnectionString"]);
            ParamList = new List<ParamEntity>();
        }
        public DataTable SelectPage(string TableName, string StrColumn, string StrWhere, string StrOrder)
        {
            int PageIndex = ValueConvert.ConvertToInt32(CloudUtil.GetFormParam("PageIndex"));
            int PageSize = ValueConvert.ConvertToInt32(CloudUtil.GetFormParam("PageSize"));
            DataTable result = SelectPage(TableName, StrColumn, StrWhere, StrOrder, PageIndex, PageSize);
            ResetSetting();
            return result;
        }
        public DataTable SelectPage(string TableName, string StrColumn, string StrWhere, string StrOrder, int PageIndex, int PageSize)
        {
            string sql = string.Format("select {0} from {1} where ", StrColumn, TableName);
            if (!string.IsNullOrEmpty(StrWhere))
            {
                sql += StrWhere;
            }
            if (!string.IsNullOrEmpty(StrOrder))
            {
                sql += " order by " + StrOrder;
            }
            if (PageIndex == 0)
            {
                PageIndex = 1;
            }
            if (PageSize != 0)
            {
                sql += string.Format(" limit {0},{1}", (PageIndex - 1) * PageSize, PageSize);
            }
            DataTable result = @CommonDao.FindViewList(sql,ParamList);
            ResetSetting();
            return result;
        }
        public DataTable Select(string TableName, string StrColumn, string StrWhere, string StrOrder)
        {
            string sql = string.Format("select {0} from {1} where  ", StrColumn, TableName);
            if (!string.IsNullOrEmpty(StrWhere))
            {
                sql += StrWhere;
            }
            if (!string.IsNullOrEmpty(StrOrder))
            {
                sql += " order by " + StrOrder;
            }
            DataTable result = @CommonDao.FindViewList(sql,ParamList);
            ResetSetting();
            return result;
        }
        public DataTable SelectGroup(string TableName, string StrColumn, string StrWhere, string StrGroup)
        {
            string sql = string.Format("select {0} from {1} where  ", StrColumn, TableName);
            if (!string.IsNullOrEmpty(StrWhere))
            {
                sql += StrWhere;
            }
            if (!string.IsNullOrEmpty(StrGroup))
            {
                sql += " group by " + StrGroup;
            }
            DataTable result = @CommonDao.FindViewList(sql, ParamList);
            ResetSetting();
            return result;
        }
        public int SelectCount(string TableName, string StrWhere)
        {
            string sql = string.Format("select count(*) from {0} where ", TableName);
            if (!string.IsNullOrEmpty(StrWhere))
            {
                sql += StrWhere;
            }
            int result =  @CommonDao.ExecuteToInt(sql, ParamList);
            ResetSetting();
            return result;
        }
        public List<TEntity> SelectPage<TEntity>(string StrColumn, string StrWhere, string StrOrder) where TEntity : BaseEntity
        {
            int PageIndex = ValueConvert.ConvertToInt32(CloudUtil.GetFormParam("PageIndex"));
            int PageSize = ValueConvert.ConvertToInt32(CloudUtil.GetFormParam("PageSize"));
            List<TEntity> result =  SelectPage<TEntity>(StrColumn, StrWhere, StrOrder, PageIndex, PageSize);
            ResetSetting();
            return result;
        }
        public List<TEntity> SelectPage<TEntity>(string StrColumn, string StrWhere, string StrOrder, int PageIndex, int PageSize) where TEntity : BaseEntity
        {
            List<TEntity> result = new List<TEntity>();
            MapperEntity mapper = StructMapping.GetInstance().GetMapperEntity(typeof(TEntity));
            DataTable dt = SelectPage(mapper.TableName, StrColumn, StrWhere, StrOrder, PageIndex, PageSize);
            foreach (DataRow item in dt.Rows)
            {
                result.Add(mapper.Map<TEntity>(item));
            }
            ResetSetting();
            return result;
        }
        public List<TEntity> Select<TEntity>(string TableName, string StrColumn, string StrWhere, string StrOrder) where TEntity : BaseEntity
        {
            List<TEntity> result = new List<TEntity>();
            MapperEntity mapper = StructMapping.GetInstance().GetMapperEntity(typeof(TEntity));
            DataTable dt = Select(TableName, StrColumn, StrWhere, StrOrder);
            foreach (DataRow item in dt.Rows)
            {
                result.Add(mapper.Map<TEntity>(item));
            }
            ResetSetting();
            return result;
        }
        public List<TEntity> Select_Cache<TEntity>()
        {
            List<TEntity> result = new List<TEntity>();
            MapperEntity mapper = StructMapping.GetInstance().GetMapperEntity(typeof(TEntity));
            Dictionary<string, object> dic = RedisUtil.GetAllHash(mapper.TableName, CloudUtil.GetRedisCacheDBNum());
            foreach (var item in dic)
            {
                result.Add(JsonConvert.DeserializeObject<TEntity>(ValueConvert.ConvertToString(item.Value)));
            }
            ResetSetting();
            return result;
        }
        public void Add2Parameter(string ParamName, object ParamValue)
        {
            ParamList.Add(new ParamEntity() { ParamName = ParamName, ParamValue = ParamValue });
        }
        public void ResetSetting()
        {
            ParamList.Clear();
        }
    }
}
